package com.taormina.searchservice;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public abstract class HttpGetSearchService extends AbstractSearchService {

	@Override
	protected InputStream getWebSearch(String query1, String query2, String query3) throws ClientProtocolException, IOException {
		
		String fullQuery = "";
		
		fullQuery = (query1 == null || query1.isEmpty()) ? fullQuery : fullQuery + query1;
		fullQuery = (query2 == null || query2.isEmpty()) ? fullQuery : fullQuery + "+" + query2;
		fullQuery = (query3 == null || query3.isEmpty()) ? fullQuery : fullQuery + "+" + query3;
		
		String searchString = getSearchUrl() + getQueryPrefix() + fullQuery;
		
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(searchString);
		HttpResponse response = client.execute(get);
		return response.getEntity().getContent();
	}
}
